<html>
	<head>
    <title>Cosmopolite Unity Demo</title>
		<script type='text/javascript' src='https://ssl-webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/jquery.min.js'></script>
		<script type='text/javascript' src='https://ssl-webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js'></script>
    <script type="text/javascript" src="/cosmopolite/static/cosmopolite.js" charset="UTF-8"></script>
  </head>
	<body>
    <div id="unityPlayer"></div>
		<script type="text/javascript">
var gameObject;
var gameModuleName;
var messageQueue = [];

var onUnityLoad = function(obj) {
  gameObject = obj.ref;
};

var sendUnityMessage = function(funcName, msg) {
  msg = msg || '';
  if (gameObject && gameModuleName) {
    gameObject.SendMessage(gameModuleName, funcName, msg);
  } else {
    messageQueue.push([funcName, msg]);
  }
};

var loginUrl, logoutUrl;

var doLogin = function() {
  if (loginUrl) {
    window.open(loginUrl);
  }
};

var doLogout = function() {
  if (logoutUrl) {
    window.open(logoutUrl);
  }
};

var registerGameObject = function(name) {
  gameModuleName = name;
  while (messageQueue.length) {
    var item = messageQueue.shift();
    sendUnityMessage(item[0], item[1]);
  }
};

var pluginDidLoad = function() {
  // Called by the Unity example code, but not used.
};

unityObject.embedUnity(
    'unityPlayer',
    'debug.unity3d',
    500,
    80,
    null,
    null,
    onUnityLoad);

var callbacks = {
  onLogin: function(username, logout_url) {
    logoutUrl = logout_url;
    sendUnityMessage('OnLogin', username);
  },
  onLogout: function(login_url) {
    loginUrl = login_url;
    sendUnityMessage('OnLogout');
  },
  onStateChange: function(key, value) {
  },
};

var cosmo = new Cosmopolite(callbacks);
		</script>
	</body>
</html>
